package com.core.family.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.core.common.utils.MyRandomUtil;
import com.core.family.admin.service.PlatPaymentService;
import com.core.family.biz.yizhao.domain.dto.WalletDto;
import com.core.family.admin.domain.req.PlatFinanceStatementReq;
import com.core.family.biz.yizhao.domain.po.YzWallet;
import com.core.family.biz.yizhao.service.YzWalletService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.core.family.admin.mapper.PlatFinanceStatementMapper;
import com.core.family.admin.domain.po.PlatFinanceStatement;
import com.core.family.admin.service.PlatFinanceStatementService;
import com.core.common.gen.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author honglei
 * @version 1.0
 * @time: 2023-7-13 15:03:14
 * @Description 服务实现层 财务账单
 */
@Slf4j
@Service
public class PlatFinanceStatementServiceImpl extends BaseServiceImpl<PlatFinanceStatementMapper,PlatFinanceStatement> implements PlatFinanceStatementService{
@Autowired
    YzWalletService walletService;

@Autowired
PlatPaymentService paymentService;

    @Override
    public WalletDto getWallet(PlatFinanceStatementReq queryReq) {
        WalletDto walletDto=new WalletDto();
        YzWallet wallet= walletService.getByUserId(queryReq.getUserId());
        if (Objects.isNull(wallet)){
          return   walletDto;
        }
        BeanUtils.copyProperties(wallet,walletDto);
        List<PlatFinanceStatement> list=selectList(queryReq);
        walletDto.setStatementList(list);
        return walletDto;
    }

    @Override
    public PlatFinanceStatement getDetailById(Long id) {
        PlatFinanceStatement statement=this.baseMapper.selectById(id);
        if (Objects.nonNull(statement)){
            statement.setPlatPayment(paymentService.getByfinanceId(statement.getId()));
        }
        return statement;
    }
    @Transactional
    @Override
    public boolean abmt(int userIdFr, int userIdTo, long price, String title, Integer orderId, boolean frozen) {
        Date date=new Date();
        String joinSn= MyRandomUtil.randomOnlySn();
        PlatFinanceStatement out=new PlatFinanceStatement()
                .setUserId(userIdFr)
                .setAuditStatus("--")
                .setBizType("支付")
                .setType("支出")
                .setOrderId(orderId)
                .setPrice((long)price)
                .setPayTime(date)
                .setPayName(title)
                .setPaySn(MyRandomUtil.randomOnlySn())
                .setStatus("已完成")
                .setAuditStatus("--")
                .setJoinSN(joinSn);

        PlatFinanceStatement in=new PlatFinanceStatement()
                .setUserId(userIdTo)
                .setAuditStatus("--")
                .setBizType("收入")
                .setType("支入")
                .setOrderId(orderId)
                .setPrice((long)price)
                .setPayTime(date)
                .setPayName(title)
                .setPaySn(MyRandomUtil.randomOnlySn())
                .setStatus("已完成")
                .setAuditStatus("--")
                .setJoinSN(joinSn);
        save(out);
        save(in);
        walletService.deduct(userIdFr,userIdTo,price,frozen);


        return true;
    }

    @Override
    public PlatFinanceStatement saveRecharge(Integer userid, String username, long longValue, String outTradeNo) {
        PlatFinanceStatement statement=new PlatFinanceStatement()
                .setUserId(userid)
                .setUserName(username)
                .setBizType("充值")
                .setType("支入")
                .setOrderDis("")
                .setPrice(longValue)
                .setPayTime(new Date())
                .setPaySn(outTradeNo)
                .setAuditStatus("--");
        save(statement);
        return statement;
    }

    @Override
    public PlatFinanceStatement getby(String paysn) {
        LambdaQueryWrapper<PlatFinanceStatement> wrapper= Wrappers.lambdaQuery();
        wrapper.eq(PlatFinanceStatement::getPaySn,paysn);
        return this.baseMapper.selectOne(wrapper);
    }


}

